package com.huluobo.a2109mvpimdemo.addGroup;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huluobo.a2109mvpimdemo.R;
import com.huluobo.a2109mvpimdemo.adapter.AddGroupAdapter;
import com.huluobo.a2109mvpimdemo.base.BaseActivity;
import com.huluobo.a2109mvpimdemo.chat.ChatActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseActivity<AddGroupContract.View, AddGroupPresenter> implements AddGroupContract.View {

    private EditText groupName, groupDesc;
    private Button createGroupBtn, findGroupBtn;

    private RecyclerView recyclerView;

    private AddGroupAdapter adapter;

    private List<EMGroupInfo> groupInfoList = new ArrayList<>();
    private String groupId;

    @Override
    public void onCreateGroupSuccess() {
        runOnUiThread(() -> Toast.makeText(AddGroupActivity.this, "创建群成功", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onCreateGroupFailed(String error) {

    }

    @Override
    public void onFindGroupSuccess(List<EMGroupInfo> value) {
        runOnUiThread(() -> {
            groupInfoList.clear();
            groupInfoList.addAll(value);
            adapter.notifyDataSetChanged();
        });
    }


    @Override
    public void onFindGroupFailed(String error) {

    }

    @Override
    public void onJoinGroupSuccess() {
        runOnUiThread(() -> Toast.makeText(AddGroupActivity.this, "加群成功", Toast.LENGTH_SHORT).show());
    }

    @Override
    public void onJoinGroupFailed(String error, int code) {
        runOnUiThread(() -> {
            if (code == 601) {
                Intent intent = new Intent(this, ChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("conversationId", groupId);
                bundle.putInt("chatType", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            } else {
                Toast.makeText(AddGroupActivity.this, "加群失败:" + error, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void initData() {
        presenter = new AddGroupPresenter();
    }

    @Override
    protected void initView() {
        groupName = findViewById(R.id.addGroup_et_groupName);
        groupDesc = findViewById(R.id.addGroup_et_groupDesc);
        createGroupBtn = findViewById(R.id.addGroup_btn_createGroup);
        findGroupBtn = findViewById(R.id.addGroup_btn_findGroup);
        recyclerView = findViewById(R.id.addGroup_rv);

        createGroupBtn.setOnClickListener(v -> {
            EMGroupOptions option = new EMGroupOptions();
            option.maxUsers = 100;
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
            presenter.createGroup(groupName.getText().toString(), groupDesc.getText().toString(),
                    new String[]{EMClient.getInstance().getCurrentUser()}
                    , "这是创建群", option);
        });

        findGroupBtn.setOnClickListener(v -> {
            presenter.findGroup(10, null);
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddGroupAdapter(groupInfoList);
        recyclerView.setAdapter(adapter);

        adapter.setOnAddClick(groupId -> {
            this.groupId = groupId;
            presenter.joinGroup(groupId);
        });
    }

    @Override
    protected int bindingLayoutId() {
        return R.layout.activity_addgroup;
    }
}